package com.ruoyi.crm.controller;

import com.ruoyi.common.annotation.MongoLog;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.domain.Clue;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.crm.domain.vo.BusinessTackVo;
import com.ruoyi.crm.domain.vo.ContractVo;
import com.ruoyi.crm.domain.vo.GivBackVo;
import com.ruoyi.crm.service.IBusinessService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("crm/businessTrack")
public class BusinessTrackController extends BaseController {

    @Resource
    private IBusinessService iBusinessService;

    @MongoLog(tableName = MongoLog.TableName.BUSINESS, title = "商机跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessTackVo businessTackVo){
        businessTackVo.setStatus(Clue.Status.TRACKING.getValue());
        return toAjax(iBusinessService.updateBusiness(businessTackVo));
    }

    /**
     * 踢回公海
     */
    @PreAuthorize("@ss.hasPermi('crm:business:edit')")
    @MongoLog(tableName = MongoLog.TableName.BUSINESS, title = "商机跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping("/givBack")
    public AjaxResult giveBack(@RequestBody GivBackVo givBackVo){
        return toAjax(iBusinessService.givBack(givBackVo));
    }

    /**
     * 捞取
     */
    @PreAuthorize("@ss.hasPermi('crm:business:edit')")
    @MongoLog(tableName = MongoLog.TableName.BUSINESS, title = "商机跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping("/reset/{id}")
    public AjaxResult reset(@RequestBody Map<String,String> body){
        return toAjax(iBusinessService.reset(Long.valueOf(body.get("id"))));
    }

    /**
     * 转客户
     * @param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('crm:business:edit')")
    @MongoLog(tableName = MongoLog.TableName.BUSINESS, title = "商机跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping("/transform")
    public AjaxResult transform(@RequestBody ContractVo contractNo) {
        return toAjax(iBusinessService.turnClient(contractNo));
    }

}
